#include "user_extern.h"

static void system_digimon_info1_base(struct digimon_st *digimon)
{
    char tmp[10] = {0};

    //digi_system.sys_ui_enable = UI_ENABLE;

    sprintf(tmp, "%s", "               ");
    oled.show_string(STRING_PADDING, STRING_POS_Y_UP, (const uint8_t *)tmp, 16, 0);
    sprintf(tmp, "%s", digimon->digi_name);
    oled.show_string(POSITION_CENTER(tmp, 16), STRING_POS_Y_UP, (const uint8_t *)tmp, 16, 0);
    //oled_scroll(0x02, 0x04, 0x05, 0x26);
    ui.draw_digimon(40, DIGIMON_Y, (void *)digimon->bmp[0]);
}

static void system_digimon_info2_weight(struct digimon_st *digimon)
{
    char tmp[10] = {0};

    sprintf(tmp, "%s", "AGE");
    oled.show_string(STRING_PADDING, STRING_POS_Y_UP, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    sprintf(tmp, "%02d", digimon->digi_age);
    oled.show_string((128 - (strlen(tmp) * 16) - 3), 0, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);

    sprintf(tmp, "%s", "WGT");
    oled.show_string(STRING_PADDING, STRING_POS_Y_DOWN, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    sprintf(tmp, "%02d", digimon->digi_weight);
    oled.show_string(POSITION_RIGHT(tmp, SYS_STRING_SIZE), STRING_POS_Y_DOWN, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
}

static void system_digimon_info3_hungry(struct digimon_st *digimon)
{
    char tmp[10] = {0};

    sprintf(tmp, "%s", "Hungry");
    oled.show_string(STRING_PADDING, STRING_POS_Y_UP, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    digimon->show_sys_aixin(digimon->digi_hungry);
}

static void system_digimon_info4_strength(struct digimon_st *digimon)
{
    char tmp[10] = {0};
    sprintf(tmp, "%s", "Strength");
    oled.show_string(STRING_PADDING, STRING_POS_Y_UP, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    digimon->show_sys_aixin(digimon->digi_strength);
}

static void system_digimon_info5_dp(struct digimon_st *digimon)
{
    char tmp[10] = {0};

    sprintf(tmp, "%s", "DP");
    oled.show_string(STRING_PADDING, STRING_POS_Y_UP, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    sprintf(tmp, "%02d/%02d", digimon->digi_dp, digimon->digi_maxdp);
    oled.show_string((SCREAM_W - 40 - STRING_PADDING), 16, (const uint8_t *)tmp, 16, 1);
    ui.draw_progress(0, 4, digimon->digi_dp, digimon->digi_maxdp);
}

static void system_digimon_info6_win(struct digimon_st *digimon)
{
    char tmp[10] = {0};

    sprintf(tmp, "%03d/%03d", digimon->digi_win, digimon->digi_battle);
    oled.show_string((SCREAM_W - 56 - STRING_PADDING), 16, (const uint8_t *)tmp, 16, 1);
    if (digimon->digi_battle == 0)
    {
        sprintf(tmp, "%d%%", 0);
        oled.show_string((128 - 32 - 3), 32, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    }
    else
    {
        sprintf(tmp, "%d%%", (digimon->digi_win / digimon->digi_battle));
        oled.show_string((128 - 32 - 3), 32, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    }
}

static void system_digimon_info7_type(struct digimon_st *digimon)
{
    char tmp[10] = {0};
    sprintf(tmp, "%s", "Type");
    oled.show_string(STRING_PADDING, STRING_POS_Y_UP, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);

    switch (digimon->digi_type)
    {
    case TYPE_NSp:
        sprintf(tmp, "%s", "Nsp");
        oled.show_string(POSITION_RIGHT(tmp, SYS_STRING_SIZE), STRING_POS_Y_UP, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
        break;
    }
    if (digimon->digi_enableB)
    {
        sprintf(tmp, "B O");
    }
    else
    {
        sprintf(tmp, "B X");
    }
    oled.show_string(STRING_PADDING, STRING_POS_Y_DOWN, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    if (digimon->digi_enableJ)
    {
        sprintf(tmp, "J O");
    }
    else
    {
        sprintf(tmp, "J X");
    }
    oled.show_string(64, STRING_POS_Y_DOWN, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
}

static void system_digimon_info8_evolution(struct digimon_st *digimon)
{
    char tmp[10] = {0};

    sprintf(tmp, "%s", "Evolutio");
    oled.show_string(STRING_PADDING, 0, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    ui.draw_progress(0, 4, digimon->digi_evolution, digimon->digi_max_evolution);
}

static void system_digimon_info9_currency(struct digimon_st *digimon)
{
    char tmp[10] = {0};

    sprintf(tmp, "%s", "Currency");
    oled.show_string(STRING_PADDING, 0, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    sprintf(tmp, "%d bit", digimon->digi_currency);
    oled.show_string(POSITION_RIGHT(tmp, SYS_STRING_SIZE), 32, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
}

uint32_t get_random(uint32_t random_seed, uint32_t min, uint32_t max)
{
    uint32_t value;

    srand(random_seed);
    value = rand() % (max + 1 - min) + min;

    return value;
}
/*********************************************************************
 *  名称： system_digimon_info
 *  功能： 显示菜单信息
 *  参数： digimon_t *digi digimon指针
 *  参数:  int page 引索
 *  返回值：void
 ********************************************************************/
void system_digimon_info(uint8_t page)
{
    //struct digimon_st *digimon = digi;
    //from_main_to_sub();

    switch (page)
    {
    case 0:
        system_digimon_info1_base(&digimon);
        break;
    case 1:
        system_digimon_info2_weight(&digimon);
        break;
    case 2:
        system_digimon_info3_hungry(&digimon);
        break;
    case 3:
        system_digimon_info4_strength(&digimon);
        break;
    case 4:
        system_digimon_info5_dp(&digimon);
        break;
    case 5:
        system_digimon_info6_win(&digimon);
        break;
    case 6:
        system_digimon_info7_type(&digimon);
        break;
    case 7:
        system_digimon_info8_evolution(&digimon);
        break;
    case 8:
        system_digimon_info9_currency(&digimon);
        break;
    }
}

/****************************************
 * 吃
*****************************************/
void system_digimon_eat_select(uint8_t food)
{
    if (Dsystem.windows_sub_id == WIN_SDIGI_MENU2_EAT)
    {
        oled.draw_bmp(32, 0, 4, 32, BMP_COVER_MODE, (void *)digi_sys_food_jitui[0]);
        oled.draw_bmp(32, 4, 4, 32, BMP_COVER_MODE, (void *)digi_sys_food_dp[0]);
    }
    else if (Dsystem.windows_sub_id == WIN_SDIGI_MENU7_HEALTH)
    {
        oled.draw_bmp(32, 0, 4, 32, BMP_COVER_MODE, (void *)digi_sys_stat_ill[0]);
        oled.draw_bmp(32, 4, 4, 32, BMP_COVER_MODE, (void *)digi_sys_stat_hurt[0]);
    }

    switch (food)
    {
    case FOOD_DRUMSTICKS:
        oled.draw_bmp(0, 0, 4, 32, BMP_COVER_MODE, (void *)digi_sys_select);
        break;
    case FOOD_POTION:
        oled.draw_bmp(0, 4, 4, 32, BMP_COVER_MODE, (void *)digi_sys_select);
        break;
    }
}

int system_digimon_eat_act(int food)
{
    static uint8_t half = 0;
    uint16_t delay_time = 200;
    uint8_t enable = 0;
    uint8_t index = 0;
    uint32_t rander = 0;
    if (Dsystem.windows_sub_id == WIN_SDIGI_MENU2_EAT)
    {
        if (food == FOOD_DRUMSTICKS)
        {
            enable = (status_cek_flag(digimon.digi_status, STA_FULL_HUNGRY) || status_cek_flag(digimon.digi_status, STA_ILL));
        }
        else if (food == FOOD_POTION)
        {
            enable = (status_cek_flag(digimon.digi_status, STA_FULL_POWER) || status_cek_flag(digimon.digi_status, STA_ILL));
        }

        if (!enable)
        {
            digimon.digimon_eat(&digimon, food);
        }
        else
        {
            digimon.show_no();
        }
    }
    else if (Dsystem.windows_sub_id == WIN_SDIGI_MENU7_HEALTH)
    {
        switch (food)
        {
        case FOOD_MEDICINE:
        {
            if (!status_cek_flag(digimon.digi_status, STA_ILL))
            {
                digimon.show_no();
            }
            else
            {
                status_del_flag(digimon.digi_status, STA_ILL);
                /* 60的概率恢复健康 */
                rander = get_random(digimon.digi_time, 0, 9);
                printf("random:%d\n", rander);
                if (rander < 6)
                {
                    digimon.digi_health = 100;
                }

                while (1)
                {
                    delay_time = 500;
                    oled.draw_bmp(40, 6, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[(index % 2) ? 7 : 0]);
                    oled.draw_bmp(SCREAM_W - 32, 0, 4, 32, BMP_COVER_MODE, (void *)digi_sys_stat_happy[index % 2]);
                    oled.refresh_all(UI_CLEAN_ON);
                    index++;
                    if (index > 6)
                    {
                        break;
                    }
                    delay_ms(delay_time);
                }
            }
            //Dsystem.windows_cur_id = SYS_MENU7_HEALTH;
        }
        break;
        case FOOD_HURT_MED:
        {
            if (!status_cek_flag(digimon.digi_status, STA_HURT))
            {
                digimon.show_no();
            }
            else
            {
                status_del_flag(digimon.digi_status, STA_HURT);

                while (1)
                {
                    delay_time = 500;
                    oled.draw_bmp(40, 6, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[(index % 2) ? 2 : 0]);
                    oled.draw_bmp(SCREAM_W - 32, 0, 4, 32, BMP_COVER_MODE, (void *)digi_sys_stat_happy[index % 2]);
                    oled.refresh_all(UI_CLEAN_ON);
                    index++;
                    if (index > 6)
                    {
                        break;
                    }
                    delay_ms(delay_time);
                }
            }
            //Dsystem.windows_cur_id = SYS_MENU7_HEALTH;
        }
        break;
        }
    }
    return 1;
}

/****************************************
 * 训练选择
*****************************************/
void system_digimon_training_select(int training_type)
{
    char tmp[10] = {0};

    sprintf(tmp, "%s", DIGIMON_TRAIN_SELECT);
    oled.show_string(POSITION_CENTER(tmp, SYS_STRING_SIZE), 0, T(tmp), SYS_STRING_SIZE, 0);
    switch (training_type)
    {
    case TRI_CLASSIC:
        sprintf(tmp, "%s", DIGIMON_TRAIN_CLASSIC);
        oled.show_string(POSITION_CENTER(tmp, SYS_STRING_SIZE), STRING_POS_Y_DOWN, T(tmp), SYS_STRING_SIZE, 1);
        break;
    case TRI_PENDULUM:
        sprintf(tmp, "%s", DIGIMON_TRAIN_PENDULUM);
        oled.show_string(POSITION_CENTER(tmp, SYS_STRING_SIZE), STRING_POS_Y_DOWN, T(tmp), SYS_STRING_SIZE, 1);
        break;
    case TRI_SEARCH:
        sprintf(tmp, "%s", DIGIMON_TRAIN_SEARCH);
        oled.show_string(POSITION_CENTER(tmp, SYS_STRING_SIZE), STRING_POS_Y_DOWN, T(tmp), SYS_STRING_SIZE, 1);
        break;
    }
}

void system_digimon_training_classic_movic(uint8_t power)
{
    uint16_t delay_time = 200;
    uint8_t index = 0;

    oled.refresh_all(UI_CLEAN_ON);
    oled.draw_bmp(0, 0, 8, 32, BMP_COVER_MODE, (void *)digi_sys_tri_classic_back);
    oled.refresh_local(0, 0, 32, 7, 0);
    {
        oled.draw_bmp_point(80, 16, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[11]);
        oled.refresh_local(80, 0, 127, 7, 1);
        delay_ms(200);
        oled.draw_bmp_point(90, 8, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[11]);
        oled.refresh_local(80, 0, 127, 7, 1);
        delay_ms(200);
        oled.draw_bmp_point(POS_X(95), 16, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[11]);
        oled.refresh_local(80, 0, 127, 7, 1);
        delay_ms(200);
    }

    while (1)
    {
        if (index < 2)
        {
            oled.draw_bmp(0, 0, 8, 32, BMP_COVER_MODE, (void *)digi_sys_tri_classic_back);
            //oled_draw_bmp(95, 6, 6, 48, (void *)digimon->bmp[11]);
            if (power == 100)
            {
                oled.draw_bmp((128 - 64 - (index % 2) * 32), 0, 4, 32, BMP_COVER_MODE, (void *)digimon.skill_bmp);
                oled.draw_bmp((128 - 64 - (index % 2) * 32), 4, 4, 32, BMP_COVER_MODE, (void *)digimon.skill_bmp);
            }
            else
            {
                oled.draw_bmp((128 - 64 - (index % 2) * 32), 0, 4, 32, BMP_COVER_MODE, (void *)digimon.skill_bmp);
            }
        }
        else if (index < 6)
        {
            delay_time = 300;
            oled.draw_bmp(0, 0, 8, 128, BMP_COVER_MODE, (void *)digi_sys_battle_back[index % 2]);
        }
        else
        {
            delay_time = 500;
            if (power == 100)
            {
                oled.draw_bmp(40, 6, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[(index % 2) ? 7 : 0]);
                oled.draw_bmp(SCREAM_W - 32, 0, 4, 32, BMP_COVER_MODE, (void *)digi_sys_stat_happy[index % 2]);
            }
            else
            {
                oled.draw_bmp(40, 6, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[(index % 2) ? 8 : 0]);
                oled.draw_bmp(SCREAM_W - 32, 0, 4, 32, BMP_COVER_MODE, (void *)digi_sys_stat_sad[index % 2]);
            }
        }
        index++;
        oled.refresh_all(UI_CLEAN_ON);

        if (index > 10)
        {
            if (power == 100)
            {
                digimon.digi_strength += 25;
                if (digimon.digi_strength >= 100)
                {
                    digimon.digi_strength = 100;
                }
            }
            digimon.digi_weight--;
            if (digimon.digi_weight <= 8)
            {
                digimon.digi_weight = 8;
            }

            digimon.digi_dp--;
            if (digimon.digi_dp < 0)
            {
                digimon.digi_dp = 0;
            }

            break;
        }
        delay_ms(delay_time);
    }
}

static void tri_classic_game(uint8_t power)
{
    char tmp[10] = {0};
    sprintf(tmp, "%s", "Power");
    oled.show_string(0, STRING_POS_Y_UP, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    sprintf(tmp, "%d%%", power);
    oled.show_string(POSITION_RIGHT(tmp, SYS_STRING_SIZE), STRING_POS_Y_DOWN, T(tmp), SYS_STRING_SIZE, 1);
}

/****************************************
 * 训练动作
*****************************************/
int system_digimon_training_act(uint8_t tri_type, uint8_t power)
{
    uint8_t ret = 0;
    char tmp[10] = {0};
    //struct digimon_st *digimon = digi;

    switch (tri_type)
    {
    case TRI_CLASSIC:
    {
        tri_classic_game(power);
        ret = 1;
    }
    break;
    case TRI_PENDULUM:
        digimon.show_no();
        ret = 0;
        break;
    case TRI_SEARCH:
        digimon.show_no();
        ret = 0;
        break;
    }

    return ret;
}

void system_digimon_battle_select(uint8_t type)
{
    char tmp[10] = {0};

    sprintf(tmp, "%s", "BATTLE");
    oled.show_string(POSITION_RIGHT(tmp, SYS_STRING_SIZE), STRING_POS_Y_UP, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);
    sprintf(tmp, "%s", "JOGRES");
    oled.show_string(POSITION_RIGHT(tmp, SYS_STRING_SIZE), STRING_POS_Y_DOWN, (const uint8_t *)tmp, SYS_STRING_SIZE, 1);

    oled.draw_bmp(0, ((type + 1) % 2) * 4, 4, 32, BMP_COVER_MODE, (void *)digi_sys_select);
}

int system_digimon_battle_act(digimon_t *digi, uint8_t tri_type)
{
#if 0
    static uint8_t index = 0;
    struct digimon_st *digimon = digi;

    if (digimon->digi_enableB == 0)
    {
        digimon_say_no(digimon);
        digi_system.sys_main_menu = SYS_MENU4_NPCBAT;
    }
#endif
    return 0;
}

int system_digimon_clean(void)
{
    uint8_t index = 0;

    if (status_cek_flag(digimon.digi_status, STA_SHIRT))
    {
        oled.draw_bmp_reversal(24, 6, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[3]);
        oled.refresh_all(UI_CLEAN_OFF);
        while (1)
        {
            oled.draw_bmp_point(96, index * 4, 4, 32, (void *)digi_sys_clean, 0);
            index++;

            //led_all_refresh_gram(UI_CLEAN_ON);
            if (index > 16)
            {
                index = 0;
                break;
            }
            oled.refresh_local(97, 0, 128, 8, UI_CLEAN_ON);
            delay_ms(10);
        }
        oled.refresh_all(UI_CLEAN_ON);
        while (1)
        {
            oled.draw_bmp(40, 6, 6, 48, BMP_COVER_MODE, (void *)digimon.bmp[(index % 2) ? 7 : 0]);
            oled.draw_bmp(SCREAM_W - 32, 0, 4, 32, BMP_COVER_MODE, (void *)digi_sys_stat_happy[index % 2]);
            oled.refresh_all(UI_CLEAN_ON);
            index++;
            if (index > 6)
            {
                break;
            }
            delay_ms(500);
        }
        if (digimon.digi_dabian > 2)
            digimon.digi_dabian -= 2;
        else
            digimon.digi_dabian = 0;
        if (digimon.digi_dabian == 0)
            status_del_flag(digimon.digi_status, STA_SHIRT);
    }
    //Dsystem.windows_cur_id =
    //digi_system.sys_sub_menu = 0;
    return 0;
}

int system_digimon_ligth_select(uint8_t onoff)
{

    char tmp[10] = {0};
    sprintf(tmp, "%s", const_on);
    oled.show_string(POSITION_RIGHT(tmp, SYS_STRING_SIZE), STRING_POS_Y_UP, T(tmp), SYS_STRING_SIZE, 1);
    sprintf(tmp, "%s", const_off);
    oled.show_string(POSITION_RIGHT(tmp, SYS_STRING_SIZE), STRING_POS_Y_DOWN, T(tmp), SYS_STRING_SIZE, 1);
    oled.draw_bmp(0, ((onoff + 1) % 2) * 4, 4, 32, BMP_COVER_MODE, (void *)digi_sys_select);
    return 0;
}

int system_digimon_ligth_act(uint8_t onoff)
{
    //struct digimon_st *digimon = digi;
    if (status_cek_flag(digimon.digi_status, STA_SLEEP))
    {
        if (onoff == ON)
        {
            status_del_flag(digimon.digi_status, STA_SLEEP);
            //digi_system.sys_main_menu = SYS_MENU0_NORMAL;
            //digi_system.sys_sub_menu = 0;
            digimon.digi_misscall++;
        }
    }
    else
    {
        if (onoff == OFF)
        {
            oled.clean_gram(0x55);
            status_set_falg(digimon.digi_status, STA_SLEEP);
        }
    }
    return 0;
}

int system_get_digimon_by_id(uint16_t id, unsigned char *bmps[48][6])
{
    init_digimon_bmps_only_one(id, bmps);
    return 0;
}

int system_digimon_dataBase(unsigned char *bmps[48][6])
{
    ui.draw_digimon(40, DIGIMON_Y, (void *)bmps[0]);
    return 0;
}